assert(PandaDKPMaster, "PandaDKPMaster not found!")

------------------------------
--      Are you local?      --
------------------------------
local PandaDKPMaster = PandaDKPMaster
local Config =
{
}
PandaDKPMaster.Config = Config


function Config:Enable()
   PandaDKPMaster.Config:PoolsListUpdate()
end

function Config:SetPanel(panel, name, parent)
   panel.name = name
   if (nil ~= parent) then panel.parent = parent end
   InterfaceOptions_AddCategory(panel)
end


function Config:ConfigPanelOnShow()
   PDKPM_ConfigSecretEdit:SetText(PandaDKPMaster.db.realm.DbPassword)
   PDKPM_ConfigDndCheck:SetChecked(PandaDKPMaster.db.realm.DoNotDisturb)
   PDKPM_ConfigItemAddingCheck:SetChecked(PandaDKPMaster.db.realm.ItemAdding)
   PDKPM_ConfigLootScanningCheck:SetChecked(PandaDKPMaster.db.realm.LootScanning)
   UIDropDownMenu_Initialize(PDKPM_ConfigThresholdMenu, function() PandaDKPMaster.Config:InitThresholdDropdown() end)
end

function Config:ConfigPanelOnHide()
   PandaDKPMaster.db.realm.DoNotDisturb = 1 == PDKPM_ConfigDndCheck:GetChecked()
   PandaDKPMaster.db.realm.ItemAdding = 1 == PDKPM_ConfigItemAddingCheck:GetChecked()
end



function Config:InitThresholdDropdown()
   local info = UIDropDownMenu_CreateInfo()
   info.owner = PDKPM_ConfigThresholdMenu
   info.func = function() PandaDKPMaster.Config:ThresholdClicked(this) end
   info.checked = nil
   
   local SetText = function(info, text, i)
      info.text = string.format("|cff%s%s|r", PandaDKPMaster:rgbToHex(ITEM_QUALITY_COLORS[i]), text)
      info.value = i
      UIDropDownMenu_AddButton(info, 1)
   end
   
   SetText(info, "Poor", 0)
   SetText(info, "Common", 1)
   SetText(info, "Uncommon", 2)
   SetText(info, "Rare", 3)
   SetText(info, "Epic", 4)
   SetText(info, "Legendary", 5)
   SetText(info, "Artifact", 6)
   
   UIDropDownMenu_SetSelectedValue(PDKPM_ConfigThresholdMenu, PandaDKPMaster.db.realm.ItemQuality)
end

function Config:ThresholdClicked(info)
   PandaDKPMaster.db.realm.ItemQuality = info.value
   UIDropDownMenu_SetSelectedValue(PDKPM_ConfigThresholdMenu, PandaDKPMaster.db.realm.ItemQuality)
end



function Config:PoolsListUpdate()
   local numPools = #(PandaDKPMaster.db.realm.DKP.Pools)
   FauxScrollFrame_Update(PDKPM_PoolsConfigPoolsScrollFrame, numPools, 16, 16, nil, 150, 150, nil, 150, 150, true)
   
   local line
   local offset
   
   for line = 1, 16 do
      offset = line + FauxScrollFrame_GetOffset(PDKPM_PoolsConfigPoolsScrollFrame)
      local button = getglobal("PDKPM_PoolsConfigPoolsLine"..line)
      button:SetID(offset)
      if (offset <= numPools) then
         local text = getglobal("PDKPM_PoolsConfigPoolsLine"..line.."Text")
         text:SetText(PandaDKPMaster.db.realm.DKP.Pools[offset])
         button:Show()
      else
         button:Hide()
      end
   end
end

function Config:PoolsListOnClick(line, button)
   if ( button == "RightButton" ) then
      local index = line:GetID()
      if (index <= #(PandaDKPMaster.db.realm.DKP.Pools)) then
         local pool = PandaDKPMaster.db.realm.DKP.Pools[index]
         local item = { { text = "Remove ".. pool, func = function() PandaDKPMaster.Pools:RemovePool(pool) end }, }
         EasyMenu(item, PDKPM_PoolsConfigPoolDropDown, "cursor", nil, nil, "MENU")
      end
   end
end


function Config:LootScanningClicked()
   if (1 == PDKPM_ConfigLootScanningCheck:GetChecked()) then
      PandaDKPMaster:Print("Turning on")
      PandaDKPMaster.db.realm.LootScanning = true
      PandaDKPMaster.Looting:TurnOnLootChecking()
   else
      PandaDKPMaster:Print("Turning off")
      PandaDKPMaster.db.realm.LootScanning = false
      PandaDKPMaster.Looting:TurnOffLootChecking()
   end
end